package com.zyh.collection;

import java.util.LinkedList;

/**
 * @Author zyh
 * @Date 2024/7/30
 * @Description LinkedList方法
 */
public class CollectionDemo3 {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.add(10);
        list.add(100);
        list.add(20);
        list.add(45);
        list.add(89);
        list.add(1234);
        System.out.println("list = " + list);//list = [10, 100, 20, 45, 89, 1234]
        //在头部添加元素
        list.addFirst(90);
        System.out.println("list = " + list);//list = [90, 10, 100, 20, 45, 89, 1234]
        //在尾部添加元素
        list.addLast(4321);
        System.out.println("list = " + list);//list = [90, 10, 100, 20, 45, 89, 1234, 4321]

        //获取头部元素
        Object first = list.getFirst();
        System.out.println("first = " + first);//first = 90
        System.out.println("list = " + list);//list = [90, 10, 100, 20, 45, 89, 1234, 4321]
        //获取尾部元素
        Object last = list.getLast();
        System.out.println("last = " + last);//last = 4321
        System.out.println("list = " + list);//list = [90, 10, 100, 20, 45, 89, 1234, 4321]

        //添加头部元素
        list.offerFirst(1234);
        System.out.println("list = " + list);//list = [1234, 90, 10, 100, 20, 45, 89, 1234, 4321]
        //添加尾部元素
        list.offerLast(1024);
        System.out.println("list = " + list);//list = [1234, 90, 10, 100, 20, 45, 89, 1234, 4321, 1024]

        //获取头部元素
        Object o = list.peekFirst();
        System.out.println("o = " + o);//o = 1234
        System.out.println("list = " + list);//list = [1234, 90, 10, 100, 20, 45, 89, 1234, 4321, 1024]
        //获取尾部元素
        Object o1 = list.peekLast();
        System.out.println("o1 = " + o1);//o1 = 1024
        System.out.println("list = " + list);//list = [1234, 90, 10, 100, 20, 45, 89, 1234, 4321, 1024]

        //获取头部元素并在集合中删除
        Object o2 = list.pollFirst();
        System.out.println("o2 = " + o2);//o2 = 1234
        System.out.println("list = " + list);//list = [90, 10, 100, 20, 45, 89, 1234, 4321, 1024]
        //获取尾部元素并在集合中删除
        Object o3 = list.pollLast();
        System.out.println("o3 = " + o3);//o3 = 1024
        System.out.println("list = " + list);//list = [90, 10, 100, 20, 45, 89, 1234, 4321]

        //在头部添加元素
        list.push(89);
        System.out.println("list = " + list);//list = [89, 90, 10, 100, 20, 45, 89, 1234, 4321]

        //获取头部元素并且集合中不删除
        Object element = list.element();
        System.out.println("element = " + element);//element = 89
        System.out.println("list = " + list);//list = [89, 90, 10, 100, 20, 45, 89, 1234, 4321]

    }
}
